package com.chy.blog_server.modular.web.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chy.blog_server.core.util.PageAndParamUtil;
import com.chy.blog_server.core.util.UploadFileUtil;
import com.chy.blog_server.core.util.UploadURLUtil;
import com.chy.blog_server.modular.web.entity.BoPersonalDiary;
import com.chy.blog_server.modular.web.service.IBoPersonalDiaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：个人日志表 controller
 * 作者：chenhuiyan
 * 时间：2020/03/03 22:07
 */
@Controller
@RequestMapping(value = "/web/boPersonalDiary")
public class BoPersonalDiaryController {

    /**
     * 前缀
     */
    private static String PREFIX = "web/boPersonalDiary/";

    @Autowired
    private IBoPersonalDiaryService boPersonalDiaryService;

    /**
     * 首页
     * @return
     */
    @RequestMapping(value = "")
    public String boContentImg(){
        return PREFIX + "boPersonalDiary";
    }

    /**
     * 列表
     * @return
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Map<String, Object> list(){
        Map<String, Object> res = new HashMap<>();
        try{
            Map<String, Object> params = PageAndParamUtil.getParams();
            List<Map<String, Object>> list = boPersonalDiaryService.list(params); //获取数据列表
            Integer count = boPersonalDiaryService.getCount(params); //获取总数
            res.put("code", 0);
            res.put("msg", "获取数据成功");
            res.put("count", count);
            res.put("data", list);
        }catch (Exception e){
            e.printStackTrace();
            res.put("code", 500);
            res.put("msg", "获取数据失败");
        }
        return res;
    }

    /**
     * 跳转添加/编辑页面
     * @return
     */
    @RequestMapping(value = "/addOrEdit")
    public String addOrEdit(String id, Model model){
        model.addAttribute("id", id);
        return PREFIX + "boPersonalDiary_add";
    }

    /**
     * 保存
     * @param boPersonalDiary
     * @param imgName 保存编辑器上传的图片名，用于记录相应的日记图片
     * @param imgSize 对应的图片大小
     * @param request
     * @return
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public Map<String, Object> saveData(BoPersonalDiary boPersonalDiary, String imgName, String imgSize, HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try {
            res = boPersonalDiaryService.saveData(boPersonalDiary, imgName, imgSize, request);
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "添加失败");
        }
        return res;
    }

    /**
     * 根据id查找信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/getInfoById")
    @ResponseBody
    public Map<String, Object> getInfoById(String id){
        Map<String, Object> res = new HashMap<>();
        try{
            res = boPersonalDiaryService.getInfoById(id);
            res.put("state", "200");
            res.put("message", "获取成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "获取失败");
        }
        return res;
    }

    /**
     * 删除
     * @param id
     * @param request
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map<String, Object> delete(String id, HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try{
            BoPersonalDiary boPersonalDiary = boPersonalDiaryService.getById(id);
            boPersonalDiary.setDelFlag(1);
            boPersonalDiary.preUpdate(request);
            boPersonalDiaryService.updateById(boPersonalDiary);
            res.put("state", "200");
            res.put("message", "删除成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "删除失败");
        }
        return res;
    }

    /**
     * 日记图片上传
     * @param image
     * @return
     */
    @RequestMapping(value = "/uploadImg")
    @ResponseBody
    public Map<String, Object> uploadImg(MultipartFile[] image){
        Map<String, Object> res = new HashMap<>();
        try {
            List<Map<String, Object>> listMap = UploadFileUtil.fileUploadToMore(image, UploadURLUtil.PERSONAL_DIARY_IMAGE, false);
            List<String> url = new ArrayList<>(); //路径+图片名
            List<String> imgName = new ArrayList<>(); //图片名
            List<String> imgSize = new ArrayList<>(); //图片大小
            for(Map map: listMap){
                if(map.get("state") == "200"){
                    url.add("/" + UploadURLUtil.PERSONAL_DIARY_IMAGE_PATH + "/" + map.get("fileName")); //返回映射路径+图片名
                    imgName.add(map.get("fileName") + ""); //返回图片名
                    imgSize.add(map.get("fileSize") + ""); //返回图片大小
                }
            }
            res.put("state", "200");
            res.put("message", "上传成功");
            res.put("url", url);
            res.put("imgName", imgName);
            res.put("imgSize", imgSize);
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "上传失败");
        }
        return res;
    }

    /**
     * 日记回收站首页
     * @return
     */
    @RequestMapping(value = "/recycleIndex")
    public String recycleIndex(){
        return PREFIX + "boPersonalDiaryRecycle";
    }

    /**
     * 删除已上传的日记图片记录
     * @param imgName
     * @return
     */
    @RequestMapping(value = "/deleteImgLog")
    @ResponseBody
    public Map<String, Object> deleteImgLog(String imgName){
        Map<String, Object> res = new HashMap<>();
        String path = UploadURLUtil.PERSONAL_DIARY_IMAGE; //路径
        try{
            if(!StrUtil.isEmpty(imgName)){
                String[] imgNameArr = imgName.split(",");
                String[] imgNamePath = new String[imgNameArr.length];
                for(int i = 0; i < imgNameArr.length; i++){
                    imgNamePath[i] = path + imgNameArr[i];
                }
                boolean flag = UploadFileUtil.deleteMoreFile(imgNamePath);
                if(flag){
                    res.put("state", "200");
                    res.put("message", "删除已上传的图片成功");
                }
            }else{
                res.put("state", "200");
                res.put("message", "没有新上传图片");
            }
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "删除已上传的图片失败");
        }
        return res;
    }

    /**
     * 查询已删除的日记列表
     * @return
     */
    @RequestMapping(value = "/recycleList")
    @ResponseBody
    public Map<String, Object> recycleList(HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try{
            Map<String, Object> params = PageAndParamUtil.getParams();
            List<Map<String, Object>> list = boPersonalDiaryService.recycleList(params); //获取数据列表
            Integer count = boPersonalDiaryService.getBaseMapper().selectCount(new QueryWrapper<BoPersonalDiary>().eq("del_flag", 1)); //获取总数
            res.put("code", 0);
            res.put("msg", "获取数据成功");
            res.put("count", count);
            res.put("data", list);
        }catch (Exception e){
            e.printStackTrace();
            res.put("code", 500);
            res.put("msg", "获取数据失败");
        }
        return res;
    }

    /**
     * 彻底删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/thoroughlyDelete")
    @ResponseBody
    public Map<String, Object> thoroughlyDelete(String id){
        Map<String, Object> res = new HashMap<>();
        try{
            int i = boPersonalDiaryService.thoroughlyDelete(id);
            if(i != 0){
                res.put("state", "200");
                res.put("message", "删除成功");
            }else {
                res.put("state", "500");
                res.put("message", "删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "删除失败");
        }
        return res;
    }

    /**
     * 恢复日记
     * @param id
     * @param request
     * @return
     */
    @RequestMapping(value = "/restore")
    @ResponseBody
    public Map<String, Object> restore(String id, HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try{
            BoPersonalDiary boContent = boPersonalDiaryService.getById(id);
            boContent.setDelFlag(0);
            boContent.preUpdate(request);
            boPersonalDiaryService.updateById(boContent);
            res.put("state", "200");
            res.put("message", "恢复成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "恢复失败");
        }
        return res;
    }

}
